﻿using Newtonsoft.Json;
using Raffles.OA.Utility;
using Raffles.OA.Data;

namespace Raffles.OA.WebApp
{
    public class OperatorProvider
    {
        private static string cookieName = ConfigHelper.GetValue("SystemKey");
        public static OperatorProvider Provider => new OperatorProvider();
        public OperatorModel GetCurrent()
        {
            return JsonConvert.DeserializeObject<OperatorModel>(DESEncrypt.Decrypt(Cookies.GetCookie(cookieName)));
        }

        public void AddCurrent(OperatorModel operatorModel)
        {
            JsonSerializerSettings jsSettings = new JsonSerializerSettings();
            jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            Cookies.WriteCookie(cookieName, DESEncrypt.Encrypt(JsonConvert.SerializeObject(operatorModel, Formatting.None, jsSettings)), int.MaxValue);
        }

        public void RemoveCurrent()
        {
            Cookies.RemoveCookie(cookieName);
        }
    }
}